﻿using Order;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;

namespace OrderForm
{
    public partial class InquiryForm : Form
    {
        Order.OrderService service;
        public InquiryForm(Order.OrderService service)
        {
            InitializeComponent();
            this.service = service;
        }


        private void Inquiry_Click(object sender, EventArgs e)
        {
            var selected = (Order.OrderService.InquiryType)selection.SelectedItem;
            try
            {
                switch (selected)
                {
                    case OrderService.InquiryType.OrderID:
                    case OrderService.InquiryType.CommodityName:
                    case OrderService.InquiryType.CustomerName:
                        list.DataSource = new BindingList<Order.Order>((service.InquiryOrders(info.Text, selected)));
                        break;
                    case OrderService.InquiryType.Total:
                        try
                        {
                            list.DataSource = new BindingList<Order.Order>(service.InquiryOrders(double.Parse(info.Text)));
                        }
                        catch (FormatException)
                        {
                            MessageBox.Show(this, "总金额无效。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        }
                        break;
                }
            }
            catch
            {
                MessageBox.Show(this, "未找到订单。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                return;
            }
        }

        private void InquiryForm_Load(object sender, EventArgs e)
        {
            selection.DataSource = new BindingList<Order.OrderService.InquiryType>() {
                OrderService.InquiryType.CommodityName,
                OrderService.InquiryType.CustomerName,
                OrderService.InquiryType.OrderID,
                OrderService.InquiryType.Total
            };
        }
    }
}
